﻿using BasicInformation.ApiWrite.Application.Command.Carrier;
using BasicInformation.Domain;
using BasicInformation.ErrorCode;
using BasicInformation.Infrastructure.Implement;
using MediatR;

namespace BasicInformation.ApiWrite.Application.Handler.Carrier
{
    public class DelCarrierHandler : IRequestHandler<DelCarrierCommand, ApiResult<int>>
    {
        private readonly ILogger<DelCarrierHandler> logger;
        private readonly DatabaseContext db;

        public DelCarrierHandler(ILogger<DelCarrierHandler> logger,DatabaseContext db)
        {
            this.logger = logger;
            this.db = db;
        }
        public async Task<ApiResult<int>> Handle(DelCarrierCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            var carrier = db.SqlSugar.Queryable<CarrierModel>().Where(x => x.CarrierId == request.CarrierId).First();
            carrier.IsDel=true;
            int r = await db.SqlSugar.Updateable(carrier).ExecuteCommandAsync();
            if (r > 0)
            {
                result.Msg = "承运商管理删除成功";
                result.Data = r;
                result.Code = ApiEnum.OK;
                logger.LogInformation(result.Msg);
            }
            else
            {
                result.Msg = "承运商管理删除失败";
                result.Data = r;
                result.Code = ApiEnum.Fail;
                logger.LogError(result.Msg);
            }
            return await Task.FromResult(result);
        }
    }
}
